<?php
class PhankenhxlsController extends AppController {

	var $name = 'Phankenhxls';
	var $helpers = array('Html', 'Form');
    var $uses=array('Kenhxl','Phankenhxl','Kenh','Phankenh');

	function index() {
	   	$this->set('kenhxls', $this->Kenhxl->find('all',array(
            'fields'=>array('Loaikenh.loaikenh','makenh','tenkenh','Kenhxl.diachi','sdt','Donvi.tendv'),
            'group'=>array('Loaikenh.loaikenh','makenh','tenkenh','Kenhxl.diachi','sdt','Donvi.tendv'))));
		
		$this->set('phankenhxls', $this->Phankenhxl->find('all'));
	}
    function luu(){
        $kenhxls=$this->Kenhxl->find('all',array(
            'fields'=>array('loaikenh_id','makenh','tenkenh','diachi','sdt','donvi_id'),
            'group'=>array('loaikenh_id','makenh','tenkenh','diachi','sdt','donvi_id')));
	   $makenh=$this->Kenh->find('list',array('fields'=>array('makenh','makenh')));
       $succeed=0;
	   $phankenhxls= $this->Phankenhxl->find('all');
	
         foreach ($kenhxls as $k){
            if(in_array($k ['Kenhxl'] ['makenh'],$makenh)){
                $sql="update kenhs set 
                        loaikenh_id=".$k ['Kenhxl'] ['loaikenh_id'].",
                        tenkenh='".$k ['Kenhxl'] ['tenkenh']."', 
                        diachi='".$k ['Kenhxl'] ['diachi']."', 
                        sdt='".$k ['Kenhxl'] ['sdt']."',
                        donvi_id=".$k ['Kenhxl'] ['donvi_id']."
                    where makenh='".$k ['Kenhxl'] ['makenh']."' 
                    ";
                if(!$this->Kenhxl->query($sql)){
                    $this->Session->setFlash(__('Có lỗi khi sửa kênh!! Chỉ lưu được '.$succeed.' dòng đầu tiên', true));
                    $this->redirect(array('controller'=>'upfiles','action'=>'add'));
                                }else{
                    $succeed++;
                }
            }
            else{
                $sql="
                    insert into kenhs (loaikenh_id,tenkenh,diachi,sdt,donvi_id,makenh) values( 
                        ".$k ['Kenhxl'] ['loaikenh_id'].",
                        '".$k ['Kenhxl'] ['tenkenh']."', 
                        '".$k ['Kenhxl'] ['diachi']."', 
                        '".$k ['Kenhxl'] ['sdt']."',
                        ".$k ['Kenhxl'] ['donvi_id'].",
                        '".$k ['Kenhxl'] ['makenh']."') 
                    ";
                //debug($sql);
                if(!$this->Kenhxl->query($sql)){
                    $this->Session->setFlash(__('Có lỗi khi thêm kênh!! Chỉ lưu được '.$succeed.' dòng đầu tiên', true));
                    $this->redirect(array('controller'=>'upfiles','action'=>'add'));
                                }else{
                    $succeed++;
                }
             }
        }
        
        $succeed=0;
        foreach($phankenhxls as $pk){
            $kenh_id=$this->Kenh->find('first',array(
                'conditions'=>array('makenh'=>$pk ['Phankenhxl'] ['makenh']),
                'fields'=>array('id'),'recursive'=>-1));
            $r ['Phankenh'] ['nhanvien_id']=$pk ['Phankenhxl'] ['nhanvien_id'];
            $r ['Phankenh'] ['kenh_id']=$kenh_id ['Kenh'] ['id'];
            $r ['Phankenh'] ['ngaybd_hd']=$pk ['Phankenhxl'] ['ngaybd_hd'];
            $this->Phankenh->create();
            if(!$this->Phankenh->saveAll($r)){
                    $this->Session->setFlash(__('Có lỗi khi phân kênh!! Chỉ lưu được '.$succeed.' dòng đầu tiên', true));
                    $this->redirect(array('controller'=>'upfiles','action'=>'add'));
                        }else{
                    $succeed++;
            }
            
        }
        
        $this->Session->setFlash(__('IMPORT dữ liệu KÊNH và PHÂN KÊNH thành công!!', true));
        $this->redirect(array('controller'=>'upfiles','action'=>'add'));
                
    }

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Phankenhxl.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('phankenhxl', $this->Phankenhxl->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Phankenhxl->create();
			if ($this->Phankenhxl->save($this->data)) {
				$this->Session->setFlash(__('The Phankenhxl has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Phankenhxl could not be saved. Please, try again.', true));
			}
		}
		$kenhs = $this->Phankenhxl->Kenh->find('list');
		$nhanviens = $this->Phankenhxl->Nhanvien->find('list');
		$this->set(compact('kenhs', 'nhanviens'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Phankenhxl', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Phankenhxl->save($this->data)) {
				$this->Session->setFlash(__('The Phankenhxl has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Phankenhxl could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Phankenhxl->read(null, $id);
		}
		$kenhs = $this->Phankenhxl->Kenh->find('list');
		$nhanviens = $this->Phankenhxl->Nhanvien->find('list');
		$this->set(compact('kenhs','nhanviens'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Phankenhxl', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Phankenhxl->del($id)) {
			$this->Session->setFlash(__('Phankenhxl deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>